﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Genesis
{
    class Sorting
    {
        public class CustomSortAsc : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                string[] px = Convert.ToString(x).Split(' ');
                string[] py = Convert.ToString(y).Split(' ');
                string newX = "";
                string newY = "";

                for (int i = 0; i < px.Length; i++)
                {
                    px[i] = px[i].Replace("{", "");
                    px[i] = px[i].Replace("}", "");
                }
                for (int i = 0; i < py.Length; i++)
                {
                    py[i] = py[i].Replace("{", "");
                    py[i] = py[i].Replace("}", "");
                }

                if ((px[1].ToLower() == "a") || (px[1].ToLower() == "the"))
                {
                    if (px.Length > 1)
                    {
                        for (int i = 2; i < px.Length; i++)
                            newX += px[i];
                    }
                }
                else
                {
                    for (int i = 1; i < px.Length; i++)
                        newX += px[i];
                }

                if ((py[1].ToLower() == "a") || (py[1].ToLower() == "the"))
                {
                    if (py.Length > 1)
                    {
                        for (int i = 2; i < py.Length; i++)
                            newY += py[i];
                    }
                }
                else
                {
                    for (int i = 1; i < py.Length; i++)
                        newY += py[i];
                }


                X = newX;
                Y = newY;

                GetX();
                GetY();

                return ((new CaseInsensitiveComparer()).Compare(X, Y));
            }
        }
        public class CustomSortDsc : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                string[] px = Convert.ToString(x).Split(' ');
                string[] py = Convert.ToString(y).Split(' ');
                string newX = "";
                string newY = "";

                for (int i = 0; i < px.Length; i++)
                {
                    px[i] = px[i].Replace("{", "");
                    px[i] = px[i].Replace("}", "");
                }
                for (int i = 0; i < py.Length; i++)
                {
                    py[i] = py[i].Replace("{", "");
                    py[i] = py[i].Replace("}", "");
                }

                if ((px[1].ToLower() == "a") || (px[1].ToLower() == "the"))
                {
                    if (px.Length > 1)
                    {
                        for (int i = 2; i < px.Length; i++)
                            newX += px[i];
                    }
                }
                else
                {
                    for (int i = 1; i < px.Length; i++)
                        newX += px[i];
                }

                if ((py[1].ToLower() == "a") || (py[1].ToLower() == "the"))
                {
                    if (py.Length > 1)
                    {
                        for (int i = 2; i < py.Length; i++)
                            newY += py[i];
                    }
                }
                else
                {
                    for (int i = 1; i < py.Length; i++)
                        newY += py[i];
                }

                X = newX;
                Y = newY;

                GetX();
                GetY();

                return ((new CaseInsensitiveComparer()).Compare(Y, X));
            }
        }

        static private void GetX()
        {
            string StrX = Convert.ToString(X);
            string StrY = Convert.ToString(Y);
            bool isXNum = false;
            bool isYNum = false;
            double NumX;
            double NumY;

            if ((StrX.Length > 0) && (StrY.Length > 0))
            {
                isXNum = double.TryParse(StrX.Substring(0, 1), out NumY);
                isYNum = double.TryParse(StrY.Substring(0, 1), out NumX);
            }

            if (isXNum == true)
            {
                if (isYNum == false)
                    X = Y;
            }
            else if (isXNum == false)
            {
                if (isYNum == true)
                    X =Y;
            }
        }
        static private void GetY()
        {
            string StrX = Convert.ToString(X);
            string StrY = Convert.ToString(Y);
            bool isXNum = false;
            bool isYNum = false;
            double NumX;
            double NumY;

            if ((StrX.Length > 0) && (StrY.Length > 0))
            {
                isXNum = double.TryParse(StrX.Substring(0, 1), out NumY);
                isYNum = double.TryParse(StrY.Substring(0, 1), out NumX);
            }

            if (isXNum == true)
            {
                if (isYNum == false)
                    Y = X;
            }
            else if (isXNum == false)
            {
                if (isYNum == true)
                    Y = X;
            }
        }
        static private object X;
        static private object Y;
    }
}
